package com.yh.homestay.controller;

import com.yh.homestay.common.JsonUtils;
import com.yh.homestay.common.RedisUtil;
import com.yh.homestay.pojo.UserStaff;
import com.yh.homestay.service.UserStaffService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName PageController
 * @Description 页面跳转Controller
 * @Date 2020/1/10 9:50
 */
@Controller
@Api(description = "页面跳转")
public class PageController {
    private Log logger = LogFactory.getLog(PageController.class);

    @Autowired
    private UserStaffService userStaffService;

    @Autowired
    private RedisUtil redisUtil;


    @Value("${REDIS.CONTENT}")
    private String redisContent;

    /**
     * @param model
     * @param request
     * @Date: 2020/1/14 17:40
     * @Description: 登录时将信息放到redis中或者从redis中获取登录信息
     * @return: java.lang.String
     */
    @RequestMapping("/")
    @ApiOperation(value = "登录", notes = "登录时将信息放到redis中或者从redis中获取登录信息")
    //从前台页面获取staffId之后，查询结果并返回对象，前台页面通过data.staffId获取
    public String toIndex(Model model, HttpServletRequest request) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Integer staffId = Integer.parseInt(authentication.getName());
        try {
            String json = (String) redisUtil.hget("redisContent", staffId + "");
            if (StringUtils.isNotBlank(json)) {
                model.addAttribute("staffId", json);
            }
        } catch (Exception e) {
            logger.info(e.getMessage());
        }
        model.addAttribute("staffId", staffId);
        request.getSession().setAttribute("staff", staffId);
        UserStaff userStaff = userStaffService.selectOneStaff(staffId);
        String s = JsonUtils.objectToJson(userStaff);
        try {
            redisUtil.hset("redisContent", staffId + "", s, 600 * 3);
        } catch (Exception e) {
            logger.info(e.getMessage());
        }
        return "index.html";
    }

    /**
     * @param
     * @Date: 2020/1/14 17:40
     * @Description: 转跳登录页面
     * @return: java.lang.String
     */
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    @ApiOperation(value = "转跳登录页面", notes = "转跳登录页面")
    public String login() {
        return "login.html";
    }

    /**
     * @param page
     * @Date: 2020/1/13 16:38
     * @Description: 页面跳转
     * @return: java.lang.String
     */
    @RequestMapping(value = "/{page}", method = RequestMethod.GET)
    public String toPage(@PathVariable("page") String page) {
        return page;
    }

    /**
     * @param request
     * @param response
     * @Date: 2020/1/8 14:03
     * @Description: 账号登出，清除cookie，session
     * @return: java.lang.String
     */
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    @ApiOperation(value = "账号登出", notes = "账号登出，清除cookie，session")
    public String login(HttpServletRequest request, HttpServletResponse response) {
        request.getSession().invalidate();
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies
        ) {
            cookie.setMaxAge(0);
            cookie.setPath("/");
            response.addCookie(cookie);
        }
        return "login";
    }
}
